<?php

namespace App\Console\Commands;

use App\Tools\Oss;
use Illuminate\Support\Facades\DB;

class DelOpusCommand extends BaseCommand
{
    /**
     * 命令行执行命令
     * @var string
     */
    protected $signature = 'delOpus';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'del opus user delete';

    private $tableName = 'opus';

    public function handle() {

        $getImgPath = function ($url) {
            $path = str_replace(env('APP_FILES_URL'), '', $url);
            return ltrim($path,"/");
        };

        return;

        $ossObj = new Oss();

        while (true){
            $list = DB::table($this->tableName)
                ->where(["delete" => 1])
                ->orderBy("id")
                ->limit(500)
                ->get(["id","url","drawing_image"])
                ->toArray();
            if (empty($list)) {
                break;
            }

            $delImgs = [];
            $ids = [];
            foreach ($list as $item) {
                $ids[] = $item->id;
                if (!empty($item->url)) {
                    $delImgs[] = $getImgPath($item->url);
                }
                if (!empty($item->drawing_image)) {
                    $delImgs[] = $getImgPath($item->drawing_image);
                }
            }

            DB::table($this->tableName)->whereIn("id",$ids)->delete();

            // 批量删除图片
            $ossObj->batchDeleteObject($delImgs);
        }
    }
}